﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using IntegrationTest.Properties;
using System.Drawing.Drawing2D;

namespace Test
{
    public partial class Wave : UserControl
    {
        public Wave()
        {
            InitializeComponent();
        }
        private List<float> points = new List<float>();
        public int MaxPtCount = Settings.Default.MaxPtCount;

        public List<float> Points
        {
            set
            {
                points = value;
                this.Invalidate();
            }
            get
            {
                return points;
            }
        }

        public void PointsClear()
        {
            points.Clear();
            this.Invalidate();
        }

        public void AddPoint(float pt)
        {
            points.Insert(0, pt);

            if (points.Count == MaxPtCount)
            {
                points.RemoveAt(MaxPtCount - 1);
            }

            this.Invalidate();
        }

        private void Wave_Paint(object sender, PaintEventArgs e)
        {

            BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;

            BufferedGraphics myBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle);

            Graphics g = myBuffer.Graphics;
            g.Clear(Settings.Default.BackColor);

            g.DrawRectangle(new Pen(Settings.Default.LineColor), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            for (int i = 0; i < 9; i++)
            {
                if (i == 4)
                {
                    g.DrawLine(new Pen(Settings.Default.CenterLineColor, 1.7f), new PointF(0, this.Height / 10.0f * (i + 1)), new PointF(this.Width, this.Height / 10.0f * (i + 1)));
                }
                else
                {
                    g.DrawLine(new Pen(Settings.Default.LineColor), new PointF(0, this.Height / 10.0f * (i + 1)), new PointF(this.Width, this.Height / 10.0f * (i + 1)));
                }
                g.DrawString((4 - i).ToString() + "g", this.Font, new SolidBrush(Settings.Default.FontColor), new PointF(0, this.Height / 10.0f * (i + 1) - 5));
            }

            DrawWave(g);

            myBuffer.Render(e.Graphics);  //呈现图像至关联的Graphics
            myBuffer.Dispose();
            g.Dispose();
        }

        private void DrawWave(Graphics g)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高，即消除锯齿
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;

            float colWidth = this.Width / (MaxPtCount + 0.0f);
            List<PointF> ps = new List<PointF>();
            for (int i = 0; i < points.Count; i++)
            {
                ps.Add(new PointF(colWidth * i + 20, (5 - points[i]) * this.Height / 10.0f));
            }
            if (ps.Count > 1)
            {
                g.DrawLines(new Pen(Settings.Default.WaveColor, 1.6f), ps.ToArray());
            }
        }

    }
}
